package com.qd.common.panda.domain.enums;

import com.cdqidi.exception.ApiException;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * 充电免费停车
 * fee_park_time1	固定免费时长
 *
 * @author sjk
 */
public enum ChargeFeeParkTimeRuleEnum {
    /**
     * 固定时长
     */
    FEE_PARK_TIME1("fee_park_time1", "固定免费时长");

    final String code;
    final String msg;

    ChargeFeeParkTimeRuleEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static void checkCode(String code) {
        if (!StringUtils.hasLength(code)) {
            throw new ApiException("充电免费停车不能为空");
        }
        if (Arrays.stream(ChargeFeeParkTimeRuleEnum.values()).filter(chargeFeeParkTimeRuleEnum -> chargeFeeParkTimeRuleEnum.getCode().equals(code)).findFirst().isEmpty()) {
            throw new ApiException("充电免费停车ID不存在");
        }
    }

    /**
     * 提前判断，用于解决
     * Case中出现的Constant expression required
     *
     * @param code 编码
     * @return 对应的枚举类
     */
    public static ChargeFeeParkTimeRuleEnum getByValue(String code) {
        for (ChargeFeeParkTimeRuleEnum carParkBillRule : values()) {
            if (carParkBillRule.getCode().equals(code)) {
                return carParkBillRule;
            }
        }
        return null;
    }
}
